<?php

namespace app\api\model\wanlshop;

use app\admin\model\activity\InformationActivity;
use app\admin\model\activity\ProjectActivity;
use app\admin\model\activity\SubjectActivity;
use app\admin\model\party\Party;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wanlshop_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
	
	// 追加属性
	protected $append = [
		'createtime_text',
	    'paymenttime_text',
	    'delivertime_text',
		'taketime_text',
	    'dealtime_text'
	];


    //订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已弃用),6=已完成,7=已取消,8=待核销（新增）
    const ORDER_STATUS_NO_PAY = 1;       // 待支付
    const ORDER_STATUS_NO_GOODS = 2;       // 待发货
    const ORDER_STATUS_NO_TAKE_GOODS = 3;       // 待收货
    const ORDER_STATUS_NO_COMMENT = 4;       // 待评论     comment
    const ORDER_STATUS_REFUSE = 5;       // 售后订单(已弃用)
    const ORDER_STATUS_FINISHED = 6;       // 已完成
    const ORDER_STATUS_CANCELED = 7;       // 已取消
    const ORDER_STATUS_NO_VERIF = 8;       // 待核销

	
	protected function setOrderNoAttr($value)
	{
	    return substr(time(),-8).substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).$value,-8);
	}
	
	protected function getStateAttr($value){
		return intval($value);
	}
	
	
	public function getCreatetimeTextAttr($value, $data)
	{
	    $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
	    return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getPaymenttimeTextAttr($value, $data)
	{
	    $value = $value ? $value : (isset($data['paymenttime']) ? $data['paymenttime'] : '');
	    return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	
	public function getDelivertimeTextAttr($value, $data)
	{
	    $value = $value ? $value : (isset($data['delivertime']) ? $data['delivertime'] : '');
	    return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	public function getTaketimeTextAttr($value, $data)
	{
	    $value = $value ? $value : (isset($data['taketime']) ? $data['taketime'] : '');
	    return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}
	
	
	public function getDealtimeTextAttr($value, $data)
	{
	    $value = $value ? $value : (isset($data['dealtime']) ? $data['dealtime'] : '');
	    return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}

	
	
	
    protected function setPaymenttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDelivertimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDealtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
	
	

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
	
	// 店铺
	public function shop()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 快递列表
	public function kuaidi()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Kuaidi', 'express_name', 'code', [], 'LEFT')->setEagerlyType(0);
	}

    //加销量数据
    public function soldNUm($order_id , $price = null)
    {
        //活动订单没法加入购物车 这只有单个订单 
      $orderGoods =  \app\admin\model\wanlshop\OrderGoods::get(['order_id'=>$order_id]);
      $order =  (new \app\api\model\wanlshop\Order())->where('id',$order_id)->find();
      $good_num = intval($orderGoods['number'])? $orderGoods['number'] : 0;
      switch ($order['type']){
          case 'subject':
              (new SubjectActivity)->where('id',$orderGoods['goods_id'])->inc('sold_num', $good_num)->update();
              break;
          case 'project':
              (new ProjectActivity())->where('id',$orderGoods['goods_id'])->inc('sold_num', $good_num)->update();
              break;
          case 'party':  // 吃喝玩乐改了逻辑  变成买单了 所以支付完成直接订单就完成了
              (new Party())->where('id',$orderGoods['goods_id'])->inc('sold_num', $good_num)->update();
              $order->state = 6;
              $order->save();
              controller('addons\wanlshop\library\WanlPay\WanlPay')->money(+$price, $order['shop']['user_id'], '用户买单', 'party', $order['order_no']);
              break;
          case 'information':
              (new InformationActivity())->where('id',$orderGoods['goods_id'])->inc('sold_num', $good_num)->update();
              break;
      }
      return true;
      
    }
	
	

	
	
	
	
}
